﻿(function (wtm) {
    wtm.form_Render = function (para) {
        if (para.beforeMount.length > 0) {
            eval(para.beforeMount + '(para);');
        }
        var $el = $('#' + para.id);
        layui.form.render(null, para.id);

        //绑定提交事件
        layui.form.on('submit(submit' + para.id + ')', function (data) {
            if (para.beforeSubmit.length > 0) {
                var rs = eval(para.beforeSubmit + '(data)');
                if (rs == false) {
                    return false;
                }
            }

            if (para.isOld == false) {
                wtm.PostForm(para.url, para.id, para.divId);
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            }
        });


        /// 渲染错误
        /// error为{key,value}对象
        if (para.errors.length > 0) {
            var err = '';
            for (var i = 0; i < para.errors.length; i++) {
                var $that = $el.find('[name="' + para.errors[i].key + '"]');
                if ($that.length > 0) {
                    layui.layer.tips(para.errors[i].value, '#' + $that.attr('id'), {
                        time: 5000,
                        tips: [2, '#FF5722']
                    });
                }
                else {
                    err += para.errors[i].value;
                }
            }

            if (err.length > 0) {
                layui.layer.msg(err, {
                    icon: 2,
                    offset: '20px',
                    shade: [0.1, 'red']
                });
            }
        }

        if (para.mounted.length > 0) {
            eval(para.mounted + '(para);');
        }
    };


    wtm.PostForm = function (url, formId, divId) {
        var layer = layui.layer;
        var index = layer.load(2);
        var reqData = wtm.GetFormData(formId);

        $.ajax({
            cache: false,
            type: "POST",
            url: url,
            data: reqData,
            async: true,
            error: function (request) {
                layer.close(index);
                alert(ff.DONOTUSE_Text_SubmitFailed);
            },
            success: function (data, textStatus, request) {
                if (request.getResponseHeader('IsScript') === 'true') {
                    eval(data);
                }
                else {
                    var inlayer = $("#" + formId).parents(".layui-layer-content");
                    if (inlayer !== undefined && inlayer.length > 0) {
                        data = "<div id='" + $.cookie("divid") + "' class='donotuse_pdiv'>" + data + "</div>";
                    } else {
                        data = "<div id='" + $.cookie("divid") + "' class='layui-card-body donotuse_pdiv'>" + data + "</div>";
                    }
                    $("#" + divId).parent().html(data);
                }
                layer.close(index);
            }
        });
    }

    //脱离ff，自我集成
    wtm.GetFormData = function (formId) {
        var richtextbox = $("#" + formId + " textarea");
        for (var i = 0; i < richtextbox.length; i++) {
            var ra = richtextbox[i].attributes['layeditindex'];
            if (ra !== undefined && ra != null) {
                var rindex = ra.value;
                layui.layedit.sync(rindex);
            }
        }
        var searchForm = $('#' + formId),
            filter = {},
            filterback = {},
            fieldElem = searchForm.find('input,select,textarea');

        var tables = $('#' + formId + ' table[id]');
        for (var i = 0; i < tables.length; i++) {
            var tableid = tables[i].id;
            var loaddata = layui.table.cache[tableid];
            if (loaddata == undefined || loaddata.length == 0) {
                var subpro = tables[i].attributes["subpro"].value;
                if (subpro != undefined && subpro != "") {
                    filter[subpro + ".length"] = "0";
                }
            }
        }

        var check = {};
        layui.each(fieldElem, function (_, item) {
            if (!item.name) return;
            if (/^checkbox$/.test(item.type) && !item.checked) {
                if (item.value === "true") {
                    filter[item.name] = false;
                }
                return;
            }
            if (/^radio$/.test(item.type) && !item.checked) {
                return;
            }
            var itemname = item.name;
            if (/_WTMMultiCombo_(.*?)_(.*?)$/.test(itemname)) {
                itemname = RegExp.$2;
            }
            if (/_DONOTUSE_(.*?)\[(\d?)\]\.(.*?)$/.test(itemname)) {
                var name1 = RegExp.$1;
                var number = RegExp.$2;
                var name2 = RegExp.$3;
                if (filterback.hasOwnProperty(name1) == false && filter.hasOwnProperty(name1 + "[" + number + "]." + name2) == false) {
                    filterback[name1] = 1;
                }
                return;
            }
            if (/_DONOTUSE_(.*?)$/.test(itemname)) {
                var name1 = RegExp.$1;
                if (filterback.hasOwnProperty(name1) == false && filter.hasOwnProperty(name1) == false) {
                    filterback[name1] = 1;
                }
                return;
            }
            var issub = false;
            if (/(.*?)\[(\-?\d?)\]\.(.*?)$/.test(itemname)) {
                var name1 = RegExp.$1;
                var number = RegExp.$2;
                var name2 = RegExp.$3;
                if (number == "-1") {
                    var checkname = itemname;
                    if (check.hasOwnProperty(checkname) == false) {
                        check[checkname] = 0;
                    }
                    if (filterback.hasOwnProperty(name1) == true) {
                        filterback[name1] = undefined;
                    }
                    if (filterback.hasOwnProperty(itemname) == true) {
                        filterback[itemname] = undefined;
                    }
                    var newname = itemname;
                    newname = name1 + "[" + check[checkname] + "]." + name2;
                    filter[newname] = item.value;
                    check[checkname] = check[checkname] + 1;
                    issub = true;
                }
            }
            if (issub == false) {
                if (filter.hasOwnProperty(itemname)) {
                    var temp = filter[itemname];
                    if (!(temp instanceof Array))
                        temp = [temp];
                    temp.push(item.value);
                    filter[itemname] = temp;
                }
                else {
                    filter[itemname] = item.value;
                    if (filterback.hasOwnProperty(itemname) == true) {
                        filterback[itemname] = undefined;
                    }
                }
            }
        });

        for (item in filterback) {
            if (filterback[item] !== undefined) {
                filter[item + ".length"] = "0";
            }
        }
        return filter;
    };

})(wtm);
